package post

import (
	"github.com/gin-gonic/gin"
	"apiserver/handler"
	"apiserver/errno"
	"apiserver/model"
	"fmt"
	"github.com/lexkong/log"
)

func Create(c *gin.Context) {
	log.Infof("创建微贴")
	var r CreateRequest
	user,_:=c.Get("userId")
	userId:=user.(uint64)
	fmt.Printf("test:%d", userId)
	if err := c.Bind(&r); err != nil {
		handler.SendResponse(c, errno.ErrBind, nil)
		return
	}
	if userId == 0 {
		handler.SendResponse(c, errno.ErrUserIdEmpty, nil)
		return
	}
	p := model.PostModel{
		Title:   r.Title,
		Content: r.Content,
		UserId:  uint64(userId),
	}
	if err := p.Create(); err != nil {
		handler.SendResponse(c, errno.ErrDatabase, nil)
		return
	}
	rsp := CreateResponse{
		Title: p.Title,
	}
	handler.SendResponse(c, nil, rsp)
	return
}
